
from rest_framework import serializers
from apps.goods.models import SPU, Brand, GoodsCategory


# SPU序列化器

class SPUserializer(serializers.ModelSerializer):

    brand = serializers.StringRelatedField(read_only=True)
    brand_id = serializers.IntegerField()
    category1_id = serializers.IntegerField()
    category2_id = serializers.IntegerField()
    category3_id = serializers.IntegerField()
    class Meta:
        model = SPU
        fields = "__all__"

        extra_kwargs = {
        'category1': {'required': False},
        'category2': {'required': False},
        'category3': {'required': False},}

    # @action(methods=['post'], detail=False)
    # def images(self,request):
    #     print("===================")
    #     image = request.data.get("image")
    #     # 验证数据
    #     if not all([image]):
    #         return Response({"detial": "数据不完整"}, status=400)
    #     # 处理
    #     image_client = Fdfs_client(settings.FASTDFS_PATH)
    #     # 上传图片   文件不在本地时用upload_by_buffer
    #     data = image_client.upload_by_buffer(image.read())
    #     image_name = data.get("Remote file_id")
    #     return image_name


# 获取品牌分类
class GoodsBrandserializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = ["id","name"]


# 获取spu一二三级分类
class SPUCategorieSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        # fields = "__all__"
        fields = ["id","name"]


